package com.ydd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ydd.constant.MessageConstant;
import com.ydd.dto.ArticlePageDTO;
import com.ydd.dto.YddArticleDTO;
import com.ydd.entity.YddArticle;
import com.ydd.mapper.ArticleMapper;
import com.ydd.mapper.CateMapper;
import com.ydd.result.PageResult;
import com.ydd.result.Result;
import com.ydd.service.ArticleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private CateMapper cateMapper;


    //修改文章
    @Override
    public Result update(YddArticleDTO yddArticleDTO) {
        //拷贝
        YddArticle yddArticle = new YddArticle();
        BeanUtils.copyProperties(yddArticleDTO, yddArticle);

        List<YddArticle> yddArticles = articleMapper.cateId(yddArticleDTO.getId());
        //判断修改文章标题是否重复
        for (YddArticle article : yddArticles) {
            if (yddArticleDTO.getTitle().equals(article.getTitle())) {
                return Result.error(MessageConstant.UNKNOWN_ERROR);
            }
        }
        articleMapper.upadte(yddArticle);
        return Result.success();
    }

    //新增文章
    @Override
    public Result saveWithFlavor(YddArticleDTO yddArticleDTO) {
        //拷贝
        YddArticle yddArticle = new YddArticle();
        BeanUtils.copyProperties(yddArticleDTO, yddArticle);
        List<YddArticle> yddArticles = articleMapper.cateId(yddArticleDTO.getId());
        //判断修改文章标题是否重复
        for (YddArticle article : yddArticles) {
            if (yddArticleDTO.getTitle().equals(article.getTitle())) {
                return Result.error(MessageConstant.UNKNOWN_ERROR);
            }
        }
        articleMapper.insertArticle(yddArticle);
        return Result.success();
    }

    //分页查询
    @Override
    public PageResult articlePageQuery(ArticlePageDTO articlePageDTO) {
        if (articlePageDTO.getPage() == null) {
            articlePageDTO.setPage(1);
        }
        if (articlePageDTO.getLimit() == null) {
            articlePageDTO.setLimit(10);
        }
//        if(articlePageDTO.getStart_time()!=null && !articlePageDTO.getStart_time().equals("")){
//            articlePageDTO.setCreate_date(Long.valueOf(articlePageDTO.getStart_time()));
//        }
//        if(articlePageDTO.getEnd_time()!=null && !articlePageDTO.getEnd_time().equals("")){
//            articlePageDTO.setUpdate_date(Long.valueOf(articlePageDTO.getEnd_time()));
//        }

        PageHelper.startPage(articlePageDTO.getPage(), articlePageDTO.getLimit());

        Page<YddArticle> page = articleMapper.pageQuery(articlePageDTO);
        page.forEach(yddArticle -> {
            yddArticle.setCatename(cateMapper.getCatename(yddArticle.getCateid()));
        });
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setData(page.getResult());
        return pageResult;
    }

    @Override
    public YddArticle rowInfo(String id) {
        YddArticle yddArticle = articleMapper.rowInfo(id);
        return yddArticle;
    }

    @Override
    public void deleteId(String id) {
        //1判断后续完善

        articleMapper.deleteId(id);
    }
}
